﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;
using System.Data.SqlClient;

namespace FYPHP
{
    public partial class Register : System.Web.UI.Page
    {
        SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["fyphp"].ConnectionString);

        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void btnSubmit_Click(object sender, EventArgs e)
        {

            try
            {

                String UserName = txtUsername.Text;
                String Password = txtPassword.Text;
                String SecretQuestion = txtSQ.Text;
                String SecretAnswer = txtSA.Text;
                String IC = txtIC.Text;
                int Age  = Convert.ToInt32(txtAge.Text);
                String Gender = rblGender.SelectedValue;
                String fullAddress = txtAdd1.Text + "|" + txtAdd2.Text + "|" + txtPostCode.Text + "|" + txtCity.Text + "|" + txtState.Text + "|" + txtCountry.Text;
                String Email = txtEmail.Text;
                String PhoneNo = txtPhone.Text;
                   

                conn.Open();

                SqlCommand cmdSearchID = new SqlCommand("Select MAX(u_id) from Users",conn);
                int u_id = Convert.ToInt32(cmdSearchID.ExecuteScalar());
                u_id++;

                Response.Write(u_id);

                SqlCommand cmdRegister = new SqlCommand("Insert into Users Values(@Id,@Username,@Password,@Ic,@Age,@Gender,@Address,@Phone,@Status,@Email,@Points,@Position,@Secretquestion,@Secretanswer,@Picture)",conn);
                cmdRegister.Parameters.AddWithValue("@Id",u_id);
                cmdRegister.Parameters.AddWithValue("@Username",UserName);
                cmdRegister.Parameters.AddWithValue("@Password",Password);
                cmdRegister.Parameters.AddWithValue("@Ic",IC);
                cmdRegister.Parameters.AddWithValue("@Age",Age);
                cmdRegister.Parameters.AddWithValue("@Gender",Gender);
                cmdRegister.Parameters.AddWithValue("@Address",fullAddress);
                cmdRegister.Parameters.AddWithValue("@Phone",PhoneNo);
                cmdRegister.Parameters.AddWithValue("@Status","Pending");
                cmdRegister.Parameters.AddWithValue("@Email",Email);
                cmdRegister.Parameters.AddWithValue("@Points",0);
                cmdRegister.Parameters.AddWithValue("@Position","Member");
                cmdRegister.Parameters.AddWithValue("@Secretquestion",SecretQuestion);
                cmdRegister.Parameters.AddWithValue("@Secretanswer",SecretAnswer);
                cmdRegister.Parameters.AddWithValue("@Picture",imgName.Text);

                cmdRegister.ExecuteNonQuery();
            
                
                conn.Close();
            }
            catch (SqlException ex)
            {
                Response.Write(ex.Message);
            }


        }

        protected void UploadButton_Click(object sender, EventArgs e)
        {
            String saveDir = @"\uploads\products\";
            string appPath = Request.PhysicalApplicationPath;

            if (FileUpload1.HasFile)
            {
                string savePath = appPath + saveDir + Server.HtmlEncode(FileUpload1.FileName);
                String fileName = FileUpload1.FileName;

                FileUpload1.SaveAs(savePath);

                UploadStatusLabel.Text = "Your file was saved as ";
                imgName.Text = fileName;
            }
            else
            {
                UploadStatusLabel.Text = "You did not specify a file to upload.";
                imgName.Text = "";
            }
        }
    }
}